home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Talking KeyBoard / Source / AppleEvents / aegetdata.c < prev    next >
Encoding:
Text File  |  1998-06-06  |  5.7 KB  |  203 lines  |  [TEXT/CWIE]

  1. // Program Author: Paul Baxter
  2. //    pbaxter@assistivetech.com
  3. //
  4. //
  5. #include <Speech.h>
  6. #include <Ctype.h>
  7. #include <DeskBus.h>
  8. #include <Retrace.h>
  9.  
  10. #include "aegetdata.h"
  11. #include "aerecord.h"
  12. #include "aeutils.h"
  13. #include "aetoken.h"
  14. #include "pref.h"
  15. #include "globals.h"
  16.  
  17. // * ****************************************************************************** *
  18. // *    DoGetData
  19. // *                            Handles the GetData AppleEvent
  20. // * ****************************************************************************** *
  21. pascal OSErr DoGetData(const AppleEvent    *theAppleEvent,
  22.                                 AppleEvent    *reply,
  23.                                 long        handlerRefCon)
  24. #pragma unused (handlerRefCon)
  25.  
  26.     AEDesc        directObj = {typeNull, NULL},
  27.                 result = {typeNull, NULL};
  28.     Size        actualSize;
  29.     DescType    returnedType;
  30.     DescType    reqType;
  31.     OSErr        reqTypeErr,
  32.                 err;
  33.  
  34.         // extract the direct object, which is the object
  35.         // whose data is to be returned
  36.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  37.     if (noErr != err) goto done;
  38.         
  39.         // now the get the type of data wanted - optional
  40.     reqTypeErr = AEGetParamPtr(theAppleEvent, keyAERequestedType, typeType,
  41.                                 &returnedType, (Ptr)&reqType, sizeof(reqType), &actualSize);
  42.         
  43.     err = GotRequiredParams(theAppleEvent);
  44.     if (noErr != err) goto done;
  45.  
  46.         // get the data
  47.     err = HandleGetData(&directObj, &result);
  48.     if (noErr != err) goto done;
  49.         // add the result
  50.     err = AddResultToReply(&result, reply, err);
  51.  
  52. done:
  53.     if (directObj.dataHandle)
  54.           AEDisposeDesc(&directObj);
  55.     if (result.dataHandle)
  56.           AEDisposeDesc(&result);
  57.         
  58.     return(err);
  59. }    // DoGetData
  60.  
  61.  
  62. // * ****************************************************************************** *
  63. // *    HandleGetData
  64. // *                    Coerces theObj into a token which we understand and
  65. // *                    extracts the data requested in the token and puts it
  66. // *                    into dataDesc.
  67. // * ****************************************************************************** *
  68. OSErr HandleGetData(AEDesc *theObj, AEDesc *dataDesc)
  69. {
  70.     AEDesc          objTokenDesc = {typeNull, NULL},
  71.                     itemDesc = {typeNull, NULL},
  72.                     resultDesc = {typeNull, NULL};
  73.     long            index;
  74.     DescType        returnedType;
  75.     OSErr           err;
  76.  
  77.             //    Coerce theObj into a token which we can use - 
  78.             //        this may involve converting a list of tokens to a list of property tokens
  79.     if (typeObjectSpecifier == theObj->descriptorType)
  80.         err = AEResolve(theObj, kAEIDoMinimum, &objTokenDesc);
  81.     else    // Otherwise, just copy it
  82.         err = AEDuplicateDesc(theObj, &objTokenDesc);
  83.     
  84.     if (noErr != err) goto done;
  85.  
  86.     switch (objTokenDesc.descriptorType) {
  87.         case typeMyApplProp:
  88.             err = GetApplicationProperty(&objTokenDesc, dataDesc);
  89.             break;
  90.  
  91.         case typeAEList:
  92.             err = AECountItems(&objTokenDesc, &index);
  93.             if (noErr != err) goto done;
  94.     
  95.             for (; index > 0; index--) {
  96.                 err = AEGetNthDesc(&objTokenDesc, index, typeWildCard, &returnedType, &itemDesc);
  97.     
  98.                 if (noErr == err)        // Call this function recursively if necessary
  99.                     err = HandleGetData(&itemDesc, &resultDesc);
  100.                 
  101.                 if (noErr == err)        // Overwrite item in list with descriptor item
  102.                     err = AEPutDesc(&objTokenDesc, index, &resultDesc);
  103.                                         // objTokenDesc is just a copy anyway.
  104.                 if (itemDesc.dataHandle)
  105.                     AEDisposeDesc(&itemDesc);
  106.                 if (resultDesc.dataHandle)
  107.                     AEDisposeDesc(&resultDesc);
  108.             }
  109.                                         // Copy list into result
  110.             err = AEDuplicateDesc(&objTokenDesc, dataDesc);
  111.             break;
  112.             
  113.         default:    
  114.             err = errAEWrongDataType;
  115.     }
  116.  
  117. done:
  118.     if (objTokenDesc.dataHandle)
  119.         AEDisposeDesc(&objTokenDesc);
  120.  
  121.     return(err);
  122. } // HandleGetData
  123.  
  124.     
  125. // * ****************************************************************************** *
  126. // *    GetApplicationProperty
  127. // *                Fills dataDesc with the requested application property.
  128. // * ****************************************************************************** *
  129. OSErr GetApplicationProperty(const AEDesc *theObjToken, AEDesc *result)
  130. {
  131.     Str255                name;
  132.     ApplPropToken        theApplPropToken;
  133.     AEDesc                aDesc = {typeNull, NULL},
  134.                         nullDesc = {typeNull, NULL};
  135.     Size                tokenSize;
  136.     DescType            theType;
  137.     ProcessInfoRec        processInfo;
  138.     ProcessSerialNumber    currentProcess;
  139.       OSErr                err;
  140.     
  141.     err = AECoerceDesc(theObjToken, typeMyApplProp, &aDesc);
  142.     if (noErr != err) goto done;
  143.  
  144.     GetRawDataFromDescriptor(&aDesc, (Ptr)&theApplPropToken,
  145.                                         sizeof(theApplPropToken), &tokenSize);
  146.       
  147.     switch (theApplPropToken.tokenApplProperty) {
  148.  
  149.         case typeVoiceProperty:
  150.             err = PutPStringToDescriptor(result, (**gPrefs).voice);
  151.             break;
  152.     
  153.         case typeSpeakCharProperty:
  154.             err = PutLongIntToDescriptor(result,  (**gPrefs).speakChars);
  155.             break;
  156.  
  157.         case typeSpeakWordProperty:
  158.             err = PutLongIntToDescriptor(result,  (**gPrefs).speakWords);
  159.             break;
  160.  
  161.         case typeSpeakSentenceProperty:
  162.             err = PutLongIntToDescriptor(result,  (**gPrefs).speakSentence);
  163.             break;
  164.  
  165.         case pBestType:      // Return the null descriptor representing
  166.         case pDefaultType:    // the application.
  167.             err = AEDuplicateDesc(&nullDesc, result);
  168.             break;
  169.  
  170.         case pClass:
  171.             theType = cApplication;
  172.             err = AECreateDesc(typeType, (Ptr)&theType, sizeof(DescType), result);
  173.             break;
  174.  
  175.         case pName:
  176.             // Clear out the name, and then call the process manager to get
  177.             // the string for the name of our application.            
  178.             name[0] = 0;
  179.             processInfo.processInfoLength = sizeof(processInfo);
  180.             processInfo.processName = name;
  181.             processInfo.processAppSpec = NULL;
  182.             
  183.             GetCurrentProcess(¤tProcess);
  184.             GetProcessInformation(¤tProcess, &processInfo);            
  185.             // Create an AEDesc returning the application name string
  186.             // returned by the process manager.
  187.             PutPStringToDescriptor(result, name);
  188.             break;
  189.  
  190.         default:
  191.             err = errAEEventNotHandled;
  192.             break;
  193.     }
  194.  
  195.  
  196. done:
  197.     if (aDesc.dataHandle)
  198.         AEDisposeDesc(&aDesc);
  199.             
  200.     return(err);
  201. } // GetApplicationProperty
  202.